package com.slipper.config;

import com.slipper.filter.HttpRequestActuatorFilter;
import com.slipper.filter.HttpRequestCorsFilter;
import com.slipper.filter.HttpRequestHtmlFilter;
import com.slipper.filter.HttpRequestLoggerFilter;
import jakarta.servlet.Filter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfiguration {
    @Value("${response.header:}")
    private String responseHeader;
    @Value("${allowed.repeat.path:}")
    private String allowedRepeatPath;

    @Bean
    public FilterRegistrationBean<HttpRequestLoggerFilter> httpRequestLoggerFilter() {
        FilterRegistrationBean<HttpRequestLoggerFilter> registration = new FilterRegistrationBean<>();
        registration.setOrder(1);
        registration.setFilter(new HttpRequestLoggerFilter(allowedRepeatPath));
        registration.addUrlPatterns("/*");
        registration.setName("httpRequestLoggerFilter");
        return registration;
    }

    @Bean
    public FilterRegistrationBean<Filter> httpRequestActuatorFilter() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setOrder(2);
        registration.setFilter(new HttpRequestActuatorFilter());
        registration.addUrlPatterns("/*");
        registration.setName("httpRequestActuatorFilter");
        return registration;
    }

    @Bean
    public FilterRegistrationBean<Filter> httpRequestCorsFilter() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setOrder(3);
        registration.setFilter(new HttpRequestCorsFilter(responseHeader));
        registration.addUrlPatterns("/*");
        registration.setName("httpRequestCorsFilter");
        return registration;
    }

    @Bean
    public FilterRegistrationBean<Filter> httpRequestHtmlFilter() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setOrder(4);
        registration.setFilter(new HttpRequestHtmlFilter());
        registration.addUrlPatterns("/", "*.html");
        registration.setName("httpRequestHtmlFilter");
        return registration;
    }
}
